﻿using Help;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLLA
{
    public partial class Order_OrganinztionCheckOutFlow_T : BaseBLL<Model.Order_OrganinztionCheckOutFlow_T>, IBLL.IOrder_OrganinztionCheckOutFlow_TBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();
        IBLL.IBLLSession bl = DI.SpringHelper.GetObject<IBLL.IBLLSession>("BLLSession");
        /// <summary>
        /// 企业平帐
        /// </summary>
        /// <param name="Data">订单ID(多选时用“,”分隔)</param>
        /// <param name="OperID">操作员ID</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式(1:json;2:xml)</param>
        /// <returns></returns>
        public string FlatOrgFlow(string Data, int OperID, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 业务流程
            //int total = 0;
            List<Model.Order_OrganinztionCheckOutFlow_T> flowList = new List<Model.Order_OrganinztionCheckOutFlow_T>();
            try
            {
                string[] ids = Data.Split(',');
                string time = DateTime.Now.ToString("yyyyMMddHHmmss");
                foreach (var item in ids)
                {
                    try
                    {
                        int formid = int.Parse(item);
                        Model.Order_FlowOrganization_T fo = base.DBSession.IOrder_FlowOrganization_TDAL.GetListBy(u=>u.FlowOrgID==formid).First();
                        fo.Flat = 1;
                        base.DBSession.IOrder_FlowOrganization_TDAL.Modify(fo);
                        Model.Order_Organization_T org = base.DBSession.IOrder_Organization_TDAL.GetListBy(u => u.OrgID == fo.OrgID).First();
                        org.OrgWaltBalance += Math.Abs( fo.OrgRepayAmount);
                        base.DBSession.IOrder_Organization_TDAL.Modify(org);
                        Model.Order_OrganinztionCheckOutFlow_T oo = new Model.Order_OrganinztionCheckOutFlow_T();  
                        oo.FormID = formid;
                        oo.OccurrenceTime = time;
                        oo.OperID = OperID;
                        oo.BZ = "";
                        flowList.Add(oo);
                    }
                    catch (Exception)
                    {
                        result.BS = "-2";
                        result.Msg = "数据异常！";
                        return os.StringOfObject(result, RETID);
                    }
                   
                }
                if (base.Add(flowList) > 0)
                {
                    // 将操作记录到日志表中
                    Model.Order_OperationFlow_T Log = new Model.Order_OperationFlow_T();
                    Log.OperationTitle = "结账信息";
                    Log.OperationText ="结账成功";
                    Log.OperationType = "1F0006";
                    Log.OperationRes = "成功";
                    Log.OperationUser = OperID;
                    string str_log = os.StringOfObject(Log, RETID);
                    bool isSuccess = new Order_OperationFlow_T().AddOrder_OperationFlow(str_log, RETID);
                    if (!isSuccess)
                    {
                        WriteLog.WriteW("商户操作结账失败");
                    }
                    result.BS = "1";
                    result.Msg = "结帐成功！";
                }
                else
                {
                    result.BS = "1";
                    result.Msg = "结帐成功！";
                }
            }
            catch (Exception)
            {
                result.BS = "-1";
                result.Msg = "数据异常！";
                return os.StringOfObject(result, RETID);
            } 
            return os.StringOfObject(result, RETID);
            #endregion
        }
    }
}
